package com.sun.filesigndemo.controller;

import com.sun.filesigndemo.bussiness.mapper.RoleMapper;
import com.sun.filesigndemo.common.pagehelper.MyPageInfo;
import com.sun.filesigndemo.common.security.model.Role;
import com.sun.filesigndemo.common.statusCode.ResultMsg;
import com.sun.filesigndemo.common.utils.JsonUtil;



import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("role")
public class RoleController {

    @Autowired
    RoleMapper roleMapper;


//    @RequiresPermissions("admin")
    @RequestMapping(value="list", method= RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> index(HttpServletRequest request, Role role) {
        //PageHelper.startPage(role.getPage(), role.getSize());
        List list = roleMapper.findAllRoleAllPermission();
        MyPageInfo<Role> roleList = new MyPageInfo<>(roleMapper.findAllRoleAllPermission());
        //MyPageInfo<Role> userList= new MyPageInfo<Role>(roleMapper.selectAll());
        return ResultMsg.successData(roleList);
    }


//    @RequiresPermissions("admin")
    @RequestMapping(method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> addRole(HttpServletRequest request,  String params){
//        Role oldrole = roleMapper.findAllRolePermissionByRolename(rolename);
//
        String date = StringEscapeUtils.unescapeHtml3(params);
        Map jsonObject = JsonUtil.parseObject(date, Map.class);

        Role role = new Role();
        role.setName(String.valueOf(jsonObject.get("rname")));

        if ((Boolean) jsonObject.get("isUpdate")) {
            int m = roleMapper.deleteAllRolePermissionByRoleId(Integer.parseInt(String.valueOf(jsonObject.get("rid"))));
            role.setRid(Integer.parseInt(String.valueOf(jsonObject.get("rid"))));
        }else{
            List<Role> olderoles = roleMapper.select(role);
            if(olderoles.size()>0){
                return ResultMsg.fail("角色名已存在");
            }
        }

        int n = 0;
        if((Boolean) jsonObject.get("isUpdate")){
            n = roleMapper.insertRole(role);
        }else {
            n = roleMapper.insertNewRole(role);
        }


        int rid = roleMapper.select(role).get(0).getRid();

        List permissions = (List) jsonObject.get("permissions");
        List list = new ArrayList();

        for(int i = 0;i<permissions.size();i++){
            HashMap rolemap = new HashMap();
            rolemap.put(role.getRid(),permissions.get(i));
            list.add(rolemap);
        }

//        for (int i = 0;i<permissions.length;i++){
//            HashMap rolemap = new HashMap();
//            rolemap.put(role.getRid(),permissions[i]);
//            list.add(rolemap);
//        }


        //roleMapper.insertPermissionToRole(role.getRid(),1);
        n = roleMapper.insertPermissionToRole(list);


        if(n>0){
            return ResultMsg.successMsg("操作成功");
        }else {
            return ResultMsg.fail("操作失败");
        }
    }

//    @RequiresPermissions("admin")
    @RequestMapping(value="DeleteRolesPermission", method= RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> deleteRolesPermission(HttpServletRequest request, String params) {
        String date = StringEscapeUtils.unescapeHtml3(params);
        Map jsonObject = JsonUtil.parseObject(date, Map.class);

        int n =roleMapper.deleteByPrimaryKey(jsonObject.get("rid"));
        int m = roleMapper.deleteAllRolePermissionByRoleId(Integer.parseInt(String.valueOf(jsonObject.get("rid"))));

       // int m = roleMapper.deleteRolesPermissionByRoleIdPermissionid(Integer.parseInt(String.valueOf(jsonObject.get("rid"))),Integer.parseInt(String.valueOf(jsonObject.get("pid"))));

        //如果某个角色的所有权限被删除 这个角色也需要被删除
//        Role role =  roleMapper.findAllRolePermissionByRolename(String.valueOf(jsonObject.get("rname")));
//        if (role == null){
//            //roleMapper.deleteAllRolePermissionByRoleId(Integer.parseInt(String.valueOf(jsonObject.get("rid"))));
//            roleMapper.deleteByPrimaryKey(jsonObject.get("rid"));
//        }

        if(n>0){
            return ResultMsg.successMsg("删除该角色成功");
        }else {
            return ResultMsg.fail("删除角色失败");
        }
    }
}
